//
//  AccountViewController.m
//  PgyHelper
//
//  Created by 鹏鹏 on 2020/11/17.
//  Copyright © 2020 CleverPeng. All rights reserved.
//

#import "AccountViewController.h"
#import "RequestTool.h"
#import "MBProgressHUD+LZPShow.h"

@interface AccountViewController ()
@property (weak, nonatomic) IBOutlet UIButton *updateButton;
@property (weak, nonatomic) IBOutlet UITextField *api_keyTF;

@end

@implementation AccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadNavigationItem];
    [self loadMainView];
}

- (void)loadNavigationItem {

}

- (void)loadMainView {
    self.updateButton.layer.cornerRadius = 4;
    self.updateButton.layer.borderWidth = 1;
    self.updateButton.layer.borderColor = UIColor.orangeColor.CGColor;
    self.updateButton.layer.masksToBounds = YES;

    self.api_keyTF.text = [RequestTool get_api_key];
}

- (IBAction)resetButtonClickAction:(id)sender {
    self.api_keyTF.text = [RequestTool reset_api_key];
}

- (IBAction)getApiKeyBtnClickedAction:(id)sender {
    NSURL *URL = [NSURL URLWithString:@"https://www.pgyer.com/account/api"];
    if ([[UIApplication sharedApplication] canOpenURL:URL]) {
        [[UIApplication sharedApplication] openURL:URL options:@{} completionHandler:nil];
    }
}

- (IBAction)getIndexPageBtnClickedAction:(id)sender {
    NSURL *URL = [NSURL URLWithString:@"https://www.pgyer.com/"];
    if ([[UIApplication sharedApplication] canOpenURL:URL]) {
        [[UIApplication sharedApplication] openURL:URL options:@{} completionHandler:nil];
    }
}

- (IBAction)updateButtonClickAction:(id)sender {
    if (self.api_keyTF.text.length == 0) {
        [MBProgressHUD showInfoOnView:self.view WithStatus:@"不得为空" afterDelay:1];
        return;
    }

    if ([RequestTool save_api_key:self.api_keyTF.text]) {
//        [MBProgressHUD showInfoOnView:self.view WithStatus:@"保存成功" afterDelay:1];
        __weak typeof(self) weakSelf = self;
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"更新" message:@"保存成功" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }]];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

@end
